package com.downhometech.productcalculator.models;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;

import com.downhometech.productcalculator.controllers.ProductController;

public class Cart 
{
	private ArrayList<CartItem> cartItems = new ArrayList<CartItem>();
	private double taxRate = 0;
	private boolean taxRateIsPercent = false;
	private double shippingRate = 0;
	private boolean shippingRateIsPercent = false;
	private double discountRate = 0;
	private double discountAmount = 0;
	private DecimalFormat doubleFormat = new DecimalFormat("##.00");
	private String checkOutDescription = "";
	
	public String formatCurrency(double currency) { return "$" + doubleFormat.format(currency); }
	public String getProductTotalFormatted() { return "$" + doubleFormat.format(getProductTotal()); }
	public String getTaxTotalFormatted() { return "$" + doubleFormat.format(getTaxTotal()); }
	public String getShippingTotalFormatted() { return "$" + doubleFormat.format(getShippingTotal()); }
	public String getTotalFormatted() { return "$" + doubleFormat.format(getTotal()); }
	
	public Cart(Context context) {
		//Load Default Settings
		ProductController controller = new ProductController(context);
		taxRate = Double.parseDouble(controller.getSettingById(Setting.DEFAULT_TAX_RATE_ID).getValue());
		taxRateIsPercent = Boolean.parseBoolean(controller.getSettingById(Setting.DEFAULT_TAX_RATE_IS_PERCENT_ID).getValue());
		shippingRate = Double.parseDouble(controller.getSettingById(Setting.DEFAULT_SHIPPING_RATE_ID).getValue());
		shippingRateIsPercent = Boolean.parseBoolean(controller.getSettingById(Setting.DEFAULT_SHIPPING_RATE_IS_PERCENT_ID).getValue());;		
		checkOutDescription = controller.getSettingById(Setting.CHECKOUT_DESCRIPTION).getValue();	

	}
	
	public Cart() { }
	
	public double getProductTotal() {
		double total = 0;
		for (int x=0; x<cartItems.size(); x++) {
			total += cartItems.get(x).getTotalPrice();
		}
		return total;
	}
	
	public double getProductTotalWithoutDiscount() {
		double total = 0;
		for (int x=0; x<cartItems.size(); x++) {
			total += cartItems.get(x).getTotalPriceWithOutDiscount();
		}
		return total;
	}
	
	public double getTaxTotal() {
		if (isTaxRateIsPercent()) {
			return (getProductTotalWithoutDiscount() + getShippingTotal()) * getTaxRate();
		}
		else {
			return getTaxRate();
		}
	}
	
	public double getShippingTotal() {
		if (isShippingRateIsPercent()) { 
			return getProductTotalWithoutDiscount() * getShippingRate();
		}
		else {
			return getShippingRate();
		}
	}
	
	public double getTotal() {

		return getProductTotal() + getTaxTotal() + getShippingTotal();
	}
	
	public int getCartItemLocationByProduct(Product prod)
	{
		for (int x=0; x<cartItems.size(); x++)
		{
			if (cartItems.get(x).getProduct().getId() == prod.getId())
			{
				return x;
			}
		}
		return -1;
	}
	
	public void changeCartDiscountForItem(Product prod, double discount)
	{
		cartItems.get(getCartItemLocationByProduct(prod)).setDiscount(discount);
	}
	
	public void changeQuantityForItem(Product prod, double quantity)
	{
		if (quantity == 0) { cartItems.remove(getCartItemLocationByProduct(prod)); }
		else { cartItems.get(getCartItemLocationByProduct(prod)).setQuantity(quantity); }
	}
	
	public void removeCartItem(CartItem item) 
	{
		int location = getCartItemLocationByProduct(item.getProduct());
		if (location > -1) { this.cartItems.remove(location); }
	}
	
	public CartItem getCartItemByProduct(Product product) 
	{
		CartItem item;
		for (int x=0; x<this.cartItems.size(); x++)
		{
			item = this.cartItems.get(x);
			if (item.getProduct().getId() == product.getId()) { return item; }
		}
		return null;
	}
	
	public void updateCartItem(CartItem item)
	{
		int cartItemLocation = getCartItemLocationByProduct(item.getProduct());
		CartItem existingItem = getCartItemByProduct(item.getProduct());
		removeCartItem(existingItem);
		this.cartItems.add(cartItemLocation, item);
	}
	
	public void addCartItem(CartItem item) 
	{
		int cartItemLocation = getCartItemLocationByProduct(item.getProduct());

		if (cartItemLocation >= 0 )
		{
			updateCartItem(item);
		}
		else if (cartItemLocation >=0 && item.getQuantity() == 0)
		{
			removeCartItem(item);
		}
		else if (item.getQuantity() > 0)
		{
			this.cartItems.add(item);
		}		
	}
	
	public ArrayList<CartItem> getCartItems() {
		return cartItems;
	}
	public void setCartItems(ArrayList<CartItem> cartItems) {
		this.cartItems = cartItems;
	}
	public double getTaxRate() {
		return taxRate;
	}
	public void setTaxRate(double taxRate) {
		this.taxRate = taxRate;
	}

	public boolean isTaxRateIsPercent() {
		return taxRateIsPercent;
	}
	public void setTaxRateIsPercent(boolean taxRateIsPercent) {
		this.taxRateIsPercent = taxRateIsPercent;
	}
	public boolean isShippingRateIsPercent() {
		return shippingRateIsPercent;
	}
	public void setShippingRateIsPercent(boolean shippingRateIsPercent) {
		this.shippingRateIsPercent = shippingRateIsPercent;
	}
	public double getShippingRate() {
		return shippingRate;
	}
	public void setShippingRate(double shippingRate) {
		this.shippingRate = shippingRate;
	}

	public double getDiscountRate() {
		return discountRate;
	}
	public void setDiscountRate(double discountRate) {
		this.discountRate = discountRate;
	}
	public double getDiscountAmount() {
		return discountAmount;
	}
	public void setDiscountAmount(double discountAmount) {
		this.discountAmount = discountAmount;
	}
	public String getCheckOutDescription() {
		return checkOutDescription;
	}
	public void setCheckOutDescription(String checkOutDescription) {
		this.checkOutDescription = checkOutDescription;
	}

	
}
